<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Cart;
use App\Order;
use App\Item;
use App\Fee;
use App\User;

class GoodsController extends Controller
{
	public function insert(){
		$data = [
		['goods_name'=>' 月季 ','goods_price'=>'23.8','goods_img'=>'goods_1.jpg'],
		['goods_name'=>' 玫瑰 ','goods_price'=>'45.6','goods_img'=>'goods_2.jpg'],
		['goods_name'=>' 桃花 ','goods_price'=>'30.8','goods_img'=>'goods_3.jpg'],
		['goods_name'=>' 妖姬 ','goods_price'=>'55.6','goods_img'=>'goods_4.jpg']
		];
		DB::table('goods')->insert($data);
	}

	public function index(){
		$goods = DB::table('goods')->get();
		return view('index',['goods'=>$goods]);
	}

	public function goods($gid){
		$goods = DB::table('goods')->where('gid',$gid)->first();
		// var_dump($goods);die;
		return view('goods',['goods_info'=>$goods]);
	}


	// 添加购物车
	public function buy($gid){
		$goods_info = DB::table('goods')->where('gid',$gid)->first();
		// 使用 Cart 类的 add 方法添加
		Cart::add($gid, $goods_info->goods_name, $goods_info->goods_price, 1, array());
		//var_dump(Cart::getContent());exit;
		// 加入成功 , 直接跳转购物车页面
		return redirect('cart');
	}

	// 查看购物车
	public function cart(){
		// 获取假如购物车内容
		$goods = Cart::getContent();
		// 共计多少钱
		$total = Cart::getTotal();
		return view('cart' , ['goods'=>$goods , 'total'=>$total]);
	}

	// 清空购物车
	public function cart_clear(){
		Cart::clear();
		return redirect('cart');
	}

	// 订单入库
	public function done(Request $req){
		// 获取用户登录信息 从 session 中取
		$u = session()->get('wechat_user');
		$openid = $u->getId();
		// 获取数据库的用户信息
		$userinfo = DB::table('users')->where('openid',$openid)->first();
		$order = new Order();
		// 共计多少钱
		$total = Cart::getTotal();
		// 写入数据库
		$order->ordsn = date('YmdHis').mt_rand(10000,99999);
		$order->uid = $userinfo->uid;
		$order->openid = $userinfo->openid;
		$order->xm = $req->xm;
		$order->address = $req->address;
		$order->tel = $req->mobile;
		$order->money = $total;
		$order->ispay = 0;
		$order->ordtime = time();
		$order->save();

		// 写入商品表信息
		// 获取假如购物车内容
		$goods = Cart::getContent();

		foreach($goods as $g){
			$item = new Item();
			$item->oid = $order->oid;
			$item->gid = $g->id;
			$item->goods_name = $g->name;
			$item->price = $g->price;
			$item->amount = $g->quantity;
			$item->save();
		}

		//清空购物车，跳转到支付页面
		$this->cart_clear();
		return view('zhifu',['oid'=>$order->oid]);

	}

	// 支付
	public function pay(Request $req){
		$oid = $req->oid;

		// 微信支付过程
		$pay_status = true;

		if ($pay_status == true) {
			DB::table('orders')->where('oid',$oid)->update(['ispay'=>1]);

			$order = Order::where('oid' , $req->oid)->first();
			$user = User::where('openid' , $order->openid)->first();
			// 为上线生成收益
			//p1 0.4 p2 0.2 p3 0.1
			$shouyi = [$user->p1,$user->p2,$user->p3];
			$yj = [0.1,0.05,0.01];
			foreach ($shouyi as $k => $v) {
				if($v > 0){
					$fee = new Fee();
					$fee->oid = $order->oid; // 订单 ID
					$fee->byid = $user->uid; // 买家 ID
					$fee->uid = $v; // 收益者
					$fee->money = $order->money * $yj[$k]; // 收益额
					$fee->save();
				}
			}
			return ' 购买成功 ';
		} else {
			return '失败';
		}

	}


}
